package com.loie.xcx.syUtil;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class PostmanCodeGenerator {




        public static void main(String[] args) {
            // 示例用法
            String url = "https://api.example.com";
            JSONArray headers = new JSONArray();
            headers.add(createHeader("Content-Type", "application/json"));
            String body = "{\"key\":\"value\"}";
            System.out.println(System.getProperty("user.dir"));
            String path=System.getProperty("user.dir")+"\\zxbbankAPI\\";

            generateAndWritePostmanCollection("",url, headers, body, path+"postman_collection.json");
        }

        public static
        JSONObject createHeader(String key, String value) {
            JSONObject header = new JSONObject();
            header.put("key", key);
            header.put("value", value);
            return header;
        }

        public static String generateAndWritePostmanCollection(String name,String url, JSONArray headers, String body, String fileName) {
            // 创建 Postman Collection 对象
            JSONObject postmanCollection = createPostmanCollection(name,url, headers, body);

            // 将 JSON 对象转换为字符串
            String jsonString = postmanCollection.toJSONString();

            // 打印生成的 JSON 字符串
            System.out.println(jsonString);

            // 将 JSON 字符串写入文件
            writeJsonToFile(jsonString, fileName);
            return jsonString;
        }

        public static JSONObject createPostmanCollection(String name,String url, JSONArray headers, String body) {
            // 创建一个简单的 Postman Collection 对象示例
//            JSONObject postmanCollection = new JSONObject();
//
//            // 设置 Collection 的基本信息
//            JSONObject info = new JSONObject();
//            info.put("name", name);
//            info.put("_postman_id", UUID.randomUUID().toString().toUpperCase());
//            info.put("description", "Description of my collection");
//            info.put("schema", "https://schema.getpostman.com/json/collection/v2.1.0/collection.json");
//            postmanCollection.put("info", info);
//
//            // 添加请求到 Collection
//            JSONArray requests = new JSONArray();
//            JSONObject request = new JSONObject();
//            request.put("method", "POST"); // 修改为 POST 方法
//            request.put("url", url);
//            request.put("description", "Example API Request");
//
//            // 添加请求头
//            request.put("header", headers);
//
//            // 添加请求体
//            JSONObject requestBody = new JSONObject();
//            requestBody.put("mode", "raw");
//            requestBody.put("raw", body);
//            request.put("body", requestBody);
//
//            requests.add(request);
//
//            postmanCollection.put("item", requests);
//
//            return postmanCollection;
            JSONObject postmanCollection = new JSONObject();

            // 设置 Collection 的基本信息
            JSONObject info = new JSONObject();
            info.put("_postman_id", UUID.randomUUID().toString().toUpperCase());
            info.put("name", name);
            info.put("schema", "https://schema.getpostman.com/json/collection/v2.1.0/collection.json");
            postmanCollection.put("info", info);

            // 添加请求到 Collection
            JSONArray items = new JSONArray();
            JSONObject item = new JSONObject();
            item.put("name", name);

            // 添加事件（Event）
//            JSONArray events = new JSONArray();
//            JSONObject event = new JSONObject();
//            event.put("listen", "test");

            // 添加事件脚本（Script）
//            JSONObject script = new JSONObject();
//            script.put("type", "text/javascript");
//            script.put("exec", getTestScriptContent());
//            event.put("script", script);
//
//            events.add(event);
//            item.put("event", events);

            // 添加请求（Request）
            JSONObject request = new JSONObject();
            request.put("method", "POST");

            // 添加请求头（Headers）
            //JSONArray headers = new JSONArray();
            request.put("header", headers);

            // 添加请求体（Body）
            JSONObject bodystr = new JSONObject();
            bodystr.put("mode", "raw");
            bodystr.put("raw", body);
            request.put("body", bodystr);

            // 添加请求 URL

            request.put("url", url);

            item.put("request", request);
            items.add(item);

            postmanCollection.put("item", items);

            return postmanCollection;
        }

        public static void writeJsonToFile(String jsonString, String fileName) {
            try (FileWriter fileWriter = new FileWriter(fileName)) {
                fileWriter.write(jsonString);
                System.out.println("JSON written to file: " + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


}
